package com.zhenbao.system.service.impl;


import com.zhenbao.system.dao.AdminMapper;
import com.zhenbao.system.pojo.Admin;
import com.zhenbao.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;


@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 增加
     * @param admin
     */
    @Override
    public void add(Admin admin) {
        if (admin!=null){
            //获取盐
            String gensalt = BCrypt.gensalt();
            //基于当前的盐对密码进行加密
            String hashpw = BCrypt.hashpw(admin.getPassword(), gensalt);
            admin.setPassword(hashpw);
            adminMapper.insert(admin);

        }

    }

    @Override
    public boolean login(Admin admin) {
        if (admin!=null){
            //根据登录名获取管理员信息
            Admin admin1 = new Admin();
            admin1.setLoginName(admin.getLoginName());
            admin1.setStatus("1");
            Admin adminResult = adminMapper.selectOne(admin1);
            //对密码进行校验
            if (adminResult == null){
                return false;
            }else {
                return BCrypt.checkpw(admin.getPassword(),adminResult.getPassword());
            }
        }else {
            return false;
        }

    }


}
